import json
import time

import scrapy

class JlangSpider(scrapy.Spider):
    name = "jlang"

    def start_requests(self):
        url = 'https://www.cninfo.com.cn/new/hisAnnouncement/query'
        for page in range(1, 101):  # 循环从第1页到第9页
            yield scrapy.FormRequest(
                url=url,
                formdata={
                    "pageNum": str(page),
                    "pageSize": "30",
                    "column": "szse",
                    "tabName": "fulltext",
                    "plate": "",
                    "stock": "",
                    "searchkey": "",
                    "secid": "",
                    "category": "",
                    "trade": "",
                    "seDate": "2024-10-16~2025-04-17",
                    "sortName": "",
                    "sortType": "",
                    "isHLtitle": "true"
                },
                callback=self.parse_index
            )

    def parse_index(self, response):
        self.log(f"=========开始请求最近公告==========")
        print(response.text)
        data = json.loads(response.text)
        for item in data['announcements']:
            yield {
                "content": item,
                "url": item['adjunctUrl'],
                "meta": {}
            }

        self.log(f"=========结束请求最近公告==========")
        time.sleep(5)


